package cn.tedu.hotel.controller;

import cn.tedu.hotel.pojo.dto.RoleAddNewDTO;
import cn.tedu.hotel.pojo.vo.RoleListItemVO;
import cn.tedu.hotel.security.LoginPrincipal;
import cn.tedu.hotel.service.IRoleService;
import cn.tedu.hotel.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "02. 角色管理模块")
@Slf4j
@RestController
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    IRoleService roleService;

    public RoleController(){
        log.debug("创建控制器：RoleController");
    }

    @ApiOperation("添加角色")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/hms/admin/update')")
    public JsonResult<Void> addNew(RoleAddNewDTO roleAddNewDTO){
        log.debug("开始处理【添加角色】的请求，参数：{}",roleAddNewDTO);
        roleService.addNew(roleAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除角色")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/hms/admin/update')")
    public JsonResult<Void> deleteById(@PathVariable Long id){
        log.debug("开始处理【删除id={}的角色】请求",id);
        roleService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("查询角色列表")
    @PreAuthorize("hasAuthority('/hms/admin/update')")
    @ApiOperationSupport(order = 401)
    @GetMapping("")
    public JsonResult<List<RoleListItemVO>> list(
            @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal
            ){
        log.debug("准备处理【查询角色列表】的请求");
        log.debug("当前登录的用户（当事人）的id：{}", loginPrincipal.getId());
        log.debug("当前登录的用户（当事人）的用户名：{}", loginPrincipal.getUsername());
        List<RoleListItemVO> list = roleService.list();
        return JsonResult.ok(list);

    }
}
